/*
* @Author: ZHELIHAO\fangshuqin
* @Date:   2018-05-21 11:40:19
* @Last Modified by:   ZHELIHAO\fangshuqin
* @Last Modified time: 2018-05-28 11:23:33
*/
{
    let EVENT_CLICK = Laya.Event.CLICK;

    class Shopdetail extends ui.Shop.ShopDetailUI {
        constructor() {
            super();

            this.init();
        }
        init() {
            this.initDom();
            this.initConfig();
            this.initEvent();
            this.registerActions();

            this.shopType = "diamondShop";
        }

        initDom() {
           this.tab_diamond = this.tab_nav.getChildByName('item0');
           this.tab_spoils  = this.tab_nav.getChildByName('item1');
           this.tab_day = this.tab_nav.getChildByName('item2');
           this.tab_coin = this.tab_nav.getChildByName('item3');

           this.list_diamond = this.tab_con.getChildByName('item0');
           this.list_spoils = this.tab_con.getChildByName('item1');
           this.list_day = this.tab_con.getChildByName('item2');
           this.list_coin = this.tab_con.getChildByName('item3');
        }

        // 初始化配置
        initConfig() {
            this.config = {
                MAX_WIDTH: 225,
                MIN_WIDTH: 21,

            }

            this.diamondList = null;
            this.spoilsList= null;
            this.dayList = null;
            this.coinList = null;

        }

        registerActions() {
            this.ACTIONS = {
                [GAME_CMDS.INSHOP]        :  this.inShop,         //进入商城
                [GAME_CMDS.DAYSHOPLIST]   :  this.dayShopList,     //每日
                [GAME_CMDS.SPOILSSHOPLIST]:  this.spoilsShopList,  //战利品
                [GAME_CMDS.COINLIST]      :  this.coinShopList,         //金币
                [GAME_CMDS.RECHARGELIST]  :  this.rechargeList,     //钻石

               
            };
            Sail.io.register(this.ACTIONS, this);
        }

        initEvent() {
            this.list_diamond_all.selectEnable = true;
            this.list_diamond_all.selectHandler = Laya.Handler.create(this, this.diamondBuy, null, false);

            this.list_spoils_all.selectEnable = true;
            this.list_spoils_all.selectHandler = Laya.Handler.create(this, this.spoilsBuy, null, false);

            this.list_day_all.selectEnable = true;
            this.list_day_all.selectHandler = Laya.Handler.create(this, this.dayBuy, null, false);

            this.list_coin_all.selectEnable = true;
            this.list_coin_all.selectHandler = Laya.Handler.create(this, this.coinBuy, null, false);

            this.tab_nav.selectHandler = Laya.Handler.create(this, this.tabSwitchHandler, null, false);
        }

        inShop(data) {
            console.log("inShop", data);
            let tab = [];
            let tabShow = [];
            let diamond =  data.diamondShop === 1 ? true : false;
            let spoils =  data.spoilsShop === 1 ? true : false;
            let day =  data.dayShop === 1 ? true : false;
            let coin = data.coinShop === 1 ? true : false;
            tab.push(diamond, spoils, day, coin);

            tab.forEach((item,index)=>{
                if(item){tabShow.push(item)}
            })

            this.bg_tab.height = 110 + 90 * (tabShow.length - 1) ;
            this.tabBox.centerY = 0;
            this.tab_diamond.visible = diamond;
            this.tab_spoils.visible  = spoils;
            this.tab_day.visible     = day;
            this.tab_coin.visible    = coin;

            Sail.io.emit(GAME_CMDS.RECHARGELIST);
        }

        diamondBuy(index) {

            let checkIndex = this.list_diamond_all.getCell(index);
            let dom_title = checkIndex.getChildByName("dom_title");

            let shopType = this.shopType;
            let bet = this.diamondList[index].rmb;
            let itemno = 'diamond/diamond_'+index;
            let title = dom_title.text;
            let type = 4;

            let cb = ()=>{
                // this.list_diamond_all.selectedIndex = -1;
            }
            Sail.director.popScene(new Alert.ShopBuy(cb, bet, shopType, itemno, title, type));
        }

        spoilsBuy(index) {
            let shopType = this.shopType;
            let bet = this.spoilsList[index].amount;
            let itemno = this.spoilsList[index].item_no;
            let title = this.spoilsList[index].name;
            let type =this.spoilsList[index].type-1;
            let number_no = this.spoilsList[index].number_no;

            let cb = ()=>{
                // this.list_spoils_all.selectedIndex = -1;
            }

            Sail.director.popScene(new Alert.ShopBuy(cb, bet, shopType, itemno, title, type, number_no));

        }

        dayBuy(index) {

            let checkIndex = this.list_day_all.getCell(index);
            let dom_colour = checkIndex.getChildByName("dom_colour");
            let dom_title = checkIndex.getChildByName("dom_title");
            let dom_itemno = checkIndex.getChildByName("dom_itemno");
            let dom_type = checkIndex.getChildByName("dom_type");
            let dom_bet = checkIndex.getChildByName("dom_bet");
            let dom_isbuy = checkIndex.getChildByName("dom_isbuy");  

            let pro_bg = checkIndex.getChildByName("pro_bg");  
            let pro_bar = checkIndex.getChildByName("pro_bg");  
            let dom_progress = checkIndex.getChildByName("dom_progress");  


            let shopType = this.shopType;
            let itemno = this.dayList[index].item_no;
            let bet = this.dayList[index].amount;
            let type =this.dayList[index].type -1;
            let title = dom_title.text;
            let number_no = this.dayList[index].number_no;
            let color = dom_colour.index;

            let showpro = false;
            let heroNum = 0;
            let maxNum = 1;
            
             if('myHeroNum' in this.dayList[index]){
                showpro = true;
                heroNum = this.dayList[index].myHeroNum;
                maxNum = this.dayList[index].maxNum;
            }


            if(dom_isbuy.visible){
                console.log("已经购买");
                Sail.director.popScene(new Alert.Tips("已经购买"));
                return;
            }

            let cb = ()=>{
                // this.list_day_all.selectedIndex = -1;
            }
            Sail.director.popScene(new Alert.ShopBuy(cb, bet, shopType, itemno, title, type, number_no, color,showpro, heroNum, maxNum));

        }

        coinBuy(index) {
            console.log("coinBuy", index);
            let checkIndex = this.list_coin_all.getCell(index);
            let dom_title = checkIndex.getChildByName("dom_title");

            let shopType = this.shopType;
            let bet = this.coinList[index].coin;

            let itemno = 'bet/'+this.coinList[index].item_no;
            let title = dom_title.text;
            let type = 1;

            let cb = ()=>{
                // this.list_coin_all.selectedIndex = -1;
            }
            Sail.director.popScene(new Alert.ShopBuy(cb, bet, shopType, itemno, title, type));
        }

        tabSwitchHandler(index) {
            console.log("index", index);
            if( index === 0 ){
                this.shopType = "diamondShop";
                Sail.io.emit(GAME_CMDS.RECHARGELIST);
            }else if( index === 1 ){
                this.shopType = "spoilsShop";
                Sail.io.emit(GAME_CMDS.SPOILSSHOPLIST);
            }else if( index === 2 ){
                this.shopType = "dayShop";
                Sail.io.emit(GAME_CMDS.DAYSHOPLIST);
            }else{
                this.shopType = "coinShop";
                Sail.io.emit(GAME_CMDS.COINLIST);
            }
            this.tab_con.selectedIndex = index;
        }


        rechargeList(data, code, msg) {
            console.log("rechargeList", data);
            if(code !==0 ) return;

            this.diamondList = data.list;
            let rechargeResult = [];

            data.list.forEach((item,index)=>{
                let first      = item.is_first >0 ? true : false;
                let adddiamond =  item.add_diamond>0 && item.is_first <=0? true :false;
                rechargeResult.push({
                    dom_title: {
                        text: '钻石X'+ (item.rmb*10) 
                    },
                    dom_itemno: {
                        skin:`res/shop/diamond/diamond_${index}.png`
                    },
                    dom_bet: {
                       text: item.rmb
                    }, 
                    dom_double:{
                        visible: first
                    },
                    dom_add: {
                        visible: adddiamond
                    },
                    dom_addtext:{
                        visible: adddiamond,
                        text:    item.add_diamond
                    }
               
                })

            })

            this.list_diamond_all.array = rechargeResult;
        }

        spoilsShopList(data, code, msg) {
            console.log("spoilsShopList", data);
            if(code !==0 ) return;

            this.spoilsList = data.list;
            let spoilsResult = [];

            data.list.forEach((item,index)=>{
                let _buyway = 0;
                if(item.type !==3 ){
                    _buyway = 1;  //购买
                }else{
                    _buyway = 0;  //兑换
                }

                spoilsResult.push({
                    dom_title: {
                        text:item.name
                    },
                    dom_itemno: {
                       skin: `res/shop/${item.item_no}.png`
                    }, 
                    dom_desc:{
                        text: item.desc
                    },
                    dom_type:{
                        index: item.type -1
                    },
                    dom_bet: {
                        text: item.amount
                    },
                    dom_button: {
                        index: _buyway
                    }
               
                })

            })

            this.list_spoils_all.array = spoilsResult;

        }

        dayShopList(data, code, msg) {
            console.log("dayShopList", data);
            if(code !==0 ) return;

            this.dayList = data.list;
            let dayResult = [];

            data.list.forEach((item,index)=>{

                // 进度
                let isShowPor = false;
                let _progress = 0;
                let upper = 0;
                let w = 0;
                let heroNum = item.myHeroNum || 0;
                let maxNum = item.maxNum || 1;
                if('myHeroNum' in item){
                    isShowPor = true;
                    _progress = heroNum /maxNum ;
                    upper = Math.floor( _progress * 100 );
                    w = upper / 100 * 280;
                    w = w < 0 ? 0 : w;
                    w = w > 280 ? 280 : w;
                }

                // 以购买  / 免费/ 购买方式
                let isBuy = false;
                let isFree = false;
                let isType = false;
                if ( item.is_buy ==1 ){
                    isBuy = true;
                    isType = false;
                    isFree = false;
                }else if( item.amount > 0 && item.is_buy ==0 ){
                    isType = true;
                    isFree = false;
                }else{
                    isFree = true;
                }
                // 彩带颜色
                let category = 0;
                if(item.category ==2 ){
                    category =0;   //紫色
                }else if(item.category ==3){
                    category =1;   //红
                }else{
                    category =2;   //绿
                }

                // item.type金币1 钻石2 勋章3 钥匙4
                dayResult.push({
                    dom_colour: {
                        index: category
                    },
                    dom_title: {
                        text:item.name + 'X' + item.num
                    },
                    dom_itemno: {
                       skin: `res/shop/${item.item_no}.png`
                    }, 
                    pro_bg: {
                        visible : isShowPor,
                    },   
                    pro_bar: {
                        width: w,
                        visible : isShowPor,
                    },
                    dom_progress:{
                        text: heroNum + '/' + maxNum,
                        visible : isShowPor,
                    },
                    dom_typebg:{
                        visible : isType
                    },
                    dom_type:{
                        visible : isType,
                        index: item.type-1
                    },
                    dom_bet: {
                        text: item.amount,
                        visible: isType
                    },
                    dom_isbuy:{
                        visible: isBuy
                    },
                    dom_isfree: {
                        visible: isFree
                    }
               
                })

            })

            this.list_day_all.array = dayResult;

            //刷新
            this.dom_num.text = data.refresh.leftNum;
            this.dom_diamond.text = data.refresh.amount;
        }

        coinShopList(data, code, msg) {
            console.log("coinList", data);
            if(code !==0 ) return;

            this.coinList = data.list;
            let coinResult = [];


            data.list.forEach((item,index)=>{
                coinResult.push({
 
                    dom_title: {
                        text:'金币X' + item.coin 
                    },
                    dom_type: {
                       skin: `res/shop/bet/${item.item_no}.png`
                    }, 
                    dom_desc:{
                        text: item.desc
                    },
                    dom_bet: {
                        text: item.diamond
                    }
               
                })

            })

            this.list_coin_all.array = coinResult;
        }



        exit() {
            Sail.io.unregister(this.ACTIONS);
        }

        resize(width, height, rate) {
            if (rate < 1) {
                this.scale(rate, rate);
            } else {
                this.scale(1, 1);
            }
        }

    }

    Sail.class(Shopdetail, "Com.Shop.Shopdetail");
}